<?php
namespace App\Handlers;
use Illuminate\Support\Facades\Storage;

/**
 * Created by PhpStorm.
 * User: leileisun
 * Date: 2019-01-01
 * Time: 10:31
 */

class ImageUploadHandler {
    protected $all_type = ['png','jpg','jpeg','gif'];

    /**
     * @param $file
     * @param $folder
     * @param $file_prefix
     * @return array|bool
     */
    public function save($file, $folder, $file_prefix) {

        $folder_name = "images/$folder/".date('Y/m/d',time());
        $subfix = strtolower($file->getClientOriginalExtension()) ?:"png";
        //$file_name = $file_prefix.'_'.time().'_'.str_random(10).'.'.$subfix;
        if (!in_array($subfix,$this->all_type)) {
            return false;
        }
        $path =  Storage::putFile($folder_name,$file);
        return [
            'path'=>$path
        ];
    }
}
